<template>
    <div class="contain-box">
        <div class="box-header">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>采购管理</el-breadcrumb-item>
                <el-breadcrumb-item to="/purchaseOrder" replace>采购单</el-breadcrumb-item>
                <el-breadcrumb-item>新增采购单</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="box-body">
            <div class="body-top">新增采购单</div>
            <div class="body-set">
                <el-input v-model="addObj.supplierName" placeholder="供应商" style="width: 200px;margin-right: 10px"
                          size="small"></el-input>
                <el-select v-model="addObj.warehousId" placeholder="请选择入库仓库" size="small"
                           style="width: 200px;margin-right: 10px">
                    <el-option v-for="item in warehousList" :key="item.id" :label="item.warehousName"
                               :value="item.id"></el-option>
                </el-select>
                <el-date-picker
                        v-model="addObj.orderCreateTime"
                        size="small"
                        value-format="yyyy-MM-dd"
                        type="date"
                        placeholder="订单日期">
                </el-date-picker>
            </div>
            <div class="body-add">
                <el-button type="primary" style="background-color: #1abc9c" size="small" @click="showGood">
                    关联商品
                </el-button>
            </div>
            <div class="body-table">
                <el-table
                        :data="chooseData"
                        size="small"
                        border
                        style="width: 100%">
                    <el-table-column
                            type="index"
                            label="序号"
                            width="100px"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="createTime"
                            label="订单日期"
                            align="center"
                    >
                    </el-table-column>

                    <el-table-column
                            prop="name"
                            label="商品名称"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="attributeContent"
                            label="规格型号"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="inventory"
                            label="数量"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="goodMeasurementUnit.name"
                            label="单位"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="price"
                            label="销售单价(元)"
                            align="center"
                    >
                    </el-table-column>

                    <el-table-column
                            label="操作"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <el-button @click="deleteObj(scope.$index)" size="small" type="text">删 除</el-button>
                        </template>
                    </el-table-column>

                </el-table>
            </div>
            <div class="body-set">
                <el-input @input="changeRate" v-model="addObj.discountRate" placeholder="折扣率" style="width: 200px;margin-right: 10px"
                          size="small"></el-input>
                <el-input disabled v-model="addObj.discountMoney" placeholder="折扣金额" style="width: 200px;margin-right: 10px"
                          size="small"></el-input>
                <el-select v-model="addObj.settlementId" placeholder="结算方式" size="small"
                           style="width: 200px;margin-right: 10px">
                    <el-option v-for="item in settlementList" :key="item.id" :label="item.settlementName"
                               :value="item.id"></el-option>
                </el-select>
            </div>
            <div class="body-set">
                <el-input v-model="addObj.orderRemarks" placeholder="请输入订单备注" type="textarea" style="width: 50%"
                          :rows="5" resize="none"
                          size="small"></el-input>
            </div>
            <div style="margin-top: 20px">
                <p style="font-size: 16px">采购合同 :</p>
                <el-upload
                        class="upload-demo"
                        :action="api+'/unitInfo/uploadFile'"
                        :on-preview="handlePreview"
                        :on-remove="handleRemove"
                        :before-remove="beforeRemove"
                        multiple
                        :headers="header"
                        :on-success="onSuccess"
                        :limit="1"
                        style="margin-top: 10px"
                        :on-exceed="handleExceed"
                        :file-list="fileList">
                    <el-button size="small" type="primary" style="background-color: #1abc9c">上 传</el-button>
                    <div slot="tip" class="el-upload__tip">（单个文件最大支持25M,支持格式Word、PDF、JPG、PNG、ZIP;)</div>
                </el-upload>
            </div>
        </div>
        <div class="box-submit">
            <div class="submit-left">
                <p>总计金额: <span>￥{{addObj.orderMoney}}</span></p>
                <p>实收金额: <span>￥{{addObj.orderMoney-addObj.discountMoney}}</span></p>
            </div>
            <div>
                <el-button @click="goBack" size="small">取 消</el-button>
                <el-button @click="submitAdd" type="primary" size="small" style="background-color: #1abc9c">提交审核</el-button>
            </div>
        </div>

        <el-dialog
                title="关联商品"
                :visible.sync="dialogTable"
                width="50%"
        >
            <div>
                <el-input clearable v-model="pageObj.name" placeholder="请输入商品名称/商品编号" style="width: 50%" size="small">
                    <el-button @click="getAllGood" slot="append" icon="el-icon-search"></el-button>
                </el-input>
                <el-table
                        :data="tableData"
                        size="mini"
                        tooltip-effect="dark"
                        style="width: 100%;margin-top: 20px"
                        @selection-change="handleSelectionChange">
                    <el-table-column
                            align="center"
                            type="selection"
                            width="55">
                    </el-table-column>
                    <el-table-column
                            prop="goodType.name"
                            label="商品分类"
                            align="center"
                    >
                    </el-table-column>

                    <el-table-column
                            prop="name"
                            label="商品名称"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="attributeContent"
                            label="规格型号"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="goodMeasurementUnit.name"
                            label="计量单位"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="inventory"
                            label="库存"
                            align="center"
                    >
                    </el-table-column>
                </el-table>
                <div class="dialog-page">
                    <el-pagination
                            background
                            @current-change="handleSelectionChange2"
                            :current-page="pageObj.pageIndex"
                            :page-size="pageObj.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="total">
                    </el-pagination>
                </div>
            </div>

            <span slot="footer" class="dialog-footer">
					<el-button @click="dialogTable = false" size="small">取 消</el-button>
					<el-button type="primary" @click="submitChoose" size="small"
                               style="background-color: #1abc9c">确 定</el-button>
				  </span>
        </el-dialog>
    </div>
</template>

<script>
    import common from "../utils/common";

    export default {
        data() {
            return {
                api: null,
                pageObj: {
                    pageIndex: 1,
                    pageSize: 5,
                    name: null
                },
                total: 0,
                settlementList: [],
                chooseData: [],
                chooseList: [],
                warehousList: [],
                dialogTable: false,
                header: {},
                tableData: [],
                fileList: [],
                addObj: {
                    orderCreateTime: null,
                    supplierName: null,
                    orderMoney: 0,
                    actualPay: 0,
                    warehousId: null,
                    goodId: [],
                    discountRate: null,
                    discountMoney: null,
                    settlementId: null,
                    orderRemarks: null,
                    purchaseContractUrl: null
                }
            }
        },
        created() {
            this.getAllSettlement()
            this.getAllWarehous()
            this.api = common.requestUrl
            this.header.token = sessionStorage.getItem('tokenValue')
        },
        methods: {
            submitAdd() {
                this.axios.post('/purchaseOrder/save', this.addObj).then(res => {
                    if (res.data.code === 200) {
                        this.goBack()
                        this.$message.success('提交成功')
                    }
                })
            },
            changeRate(){
                if (this.addObj.discountRate) {
                    let m = this.addObj.orderMoney * this.addObj.discountRate
                    if (m > this.addObj.orderMoney) {
                        this.addObj.discountRate = null
                        this.addObj.discountMoney = 0
                        this.submitChoose()
                        return this.$message.warning('折扣金额不能大于总金额')
                    }
                    this.addObj.discountMoney = this.addObj.orderMoney * this.addObj.discountRate
                    this.addObj.actualPay = this.addObj.orderMoney - this.addObj.discountMoney
                } else {
                    // this.addObj.discountMoney = 0
                    this.submitChoose()
                }
            },
            deleteObj(index) {
                this.chooseData.splice(index, 1)
                this.addObj.goodId = []
                this.addObj.orderMoney = 0
                this.addObj.actualPay = 0
                this.addObj.discountMoney = 0
                if (this.chooseData.length > 0) {
                    for (let i = 0; i < this.chooseData.length; i++) {
                        this.addObj.goodId.push(this.chooseData[i].id)
                        this.addObj.orderMoney = this.addObj.orderMoney + this.chooseData[i].price
                        this.addObj.actualPay = this.addObj.actualPay + this.chooseData[i].price
                    }
                    if (this.addObj.discountRate) {
                        this.addObj.discountMoney = this.addObj.orderMoney * this.addObj.discountMoney
                        this.addObj.actualPay = this.addObj.orderMoney - this.addObj.discountMoney
                    }
                }
            },
            submitChoose() {
                this.chooseData = this.chooseList
                this.addObj.goodId = []
                this.addObj.orderMoney = 0
                this.addObj.actualPay = 0
                this.addObj.discountMoney = 0
                if (this.chooseData.length > 0) {
                    for (let i = 0; i < this.chooseData.length; i++) {
                        this.addObj.goodId.push(this.chooseData[i].id)
                        this.addObj.orderMoney = this.addObj.orderMoney + this.chooseData[i].price
                        this.addObj.actualPay = this.addObj.actualPay + this.chooseData[i].price
                    }
                    if (this.addObj.discountRate) {
                        this.addObj.discountMoney = this.addObj.orderMoney * this.addObj.discountRate
                        this.addObj.actualPay = this.addObj.orderMoney - this.addObj.discountMoney
                    }
                }
                this.dialogTable = false
            },
            onSuccess(response, file, fileList) {
                if (response.code === 200) {
                    this.addObj.purchaseContractUrl = response.data
                    this.$message.success(response.message)
                } else {
                    this.$message.warning(response.message)
                }
            },
            handleSelectionChange2(currentPage) {
                this.pageObj.pageIndex = currentPage;
                this.getAllGood();
            },
            showGood() {
                this.getAllGood()
            },
            //查询所有商品
            getAllGood() {
                this.axios.post('/good/findPage', this.pageObj).then(res => {
                    if (res.data.code === 200) {
                        this.tableData = res.data.data.data
                        this.total = res.data.data.total
                        this.dialogTable = true
                    }
                })
            },
            goBack() {
                this.$router.go(-1)
            },
            handleSelectionChange(val) {
                console.log(val)
                this.chooseList = val
            },
            handleRemove(file, fileList) {
                this.fileList = []
                console.log(file, fileList);
            },
            handlePreview(file) {
                console.log(file);
            },
            handleExceed(files, fileList) {
                this.$message.warning('当前限制选择 1 个文件');
            },
            beforeRemove(file, fileList) {
                return this.$confirm(`确定移除 ${file.name}？`);
            },
            //查询全部结算方式
            getAllSettlement() {
                this.axios.get('/settlement/findAll').then(res => {
                    if (res.data.code === 200) {
                        this.settlementList = res.data.data
                    }
                })
            },
            //查询全部仓库
            getAllWarehous() {
                this.axios.get('/warehous/findAll').then(res => {
                    if (res.data.code === 200) {
                        this.warehousList = res.data.data
                    }
                })
            },
        }
    }
</script>

<style scoped lang="less">
    .contain-box {
        width: 100%;
        height: 100%;

        /deep/ .distpicker-address-wrapper {
            select {
                width: 32% !important;
                padding: 0px 10px !important;
                height: 35px !important;
                font-size: 12px !important;
                line-height: 35px !important;
                border-radius: 5px;
            }
        }

        .dialog-box {
            width: 100%;
        }

        .dialog-page {
            margin-top: 20px;
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: flex-end;

            /deep/ .el-pagination.is-background .el-pager li:not(.disabled).active {
                background-color: #1abc9c;
            }
        }

        /deep/ .el-dialog__body {
            padding: 10px 20px;
        }

        .box-header {
            width: 100%;
            height: 50px;
            display: flex;
            align-items: center;
            margin-bottom: 10px;
        }

        .box-body {
            width: 100%;
            border-radius: 5px;
            padding: 20px;
            box-sizing: border-box;
            box-shadow: 0 0 5px 0 #e4e4e4;

            .body-top {
                width: 100%;
                height: 50px;
                text-align: center;
                line-height: 50px;
                border-bottom: 1px solid #e4e4e4;
                font-size: 20px;
                color: #666;
            }

            .body-set {
                margin-top: 20px;
                width: 100%;
                display: flex;
                align-items: center;
                justify-content: flex-start;
            }

            .body-add {
                margin-top: 20px;
                width: 100%;
            }

            .body-table {
                margin-top: 20px;
                width: 100%;
            }

            .body-message {
                margin-top: 20px;
                width: 100%;

                .message-box {
                    margin-top: 20px;
                    display: flex;
                    align-items: center;
                    cursor: pointer;

                    .message-img {
                        margin-left: 20px;
                        width: 20px;
                        height: 20px;
                    }

                    .message-son {
                        margin-right: 15px;
                        font-size: 14px;
                    }
                }
            }
        }

        .box-submit {
            margin-top: 20px;
            width: 100%;
            border-radius: 5px;
            padding: 20px;
            box-sizing: border-box;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 0 5px 0 #e4e4e4;

            .submit-left {
                display: flex;
                font-size: 16px;

                p {
                    margin-right: 20px;

                    span {
                        color: red;
                        font-weight: bold;
                    }
                }
            }
        }
    }
</style>
